<h1>Upload users</h1>

<p>Firstly, note that <strong>it is  usually not necessary to import users in bulk</strong> - to keep your own maintenance work down
you should first explore forms of authentication that do not require manual maintenance, such as connecting to existing external
databases or letting the users create their own accounts. See the Authentication section in the admin menus.</p>
<p>If you are sure you want to import multiple user accounts from a text file, then you need to format your text file as follows:</p>

<ul>
  <li>Each line of the file contains one record</li>
  <li>Each record is a series of data separated by commas (or other delimiters)</li>
  <li>The first record of the file is special, and contains a list of field names. This defines the format of the rest of the file.
    <blockquote>
      <p><strong>Required field names:</strong> these fields must be included in the first record, and defined for each user</p>
      <p><code>firstname, lastname</code> when inserting or <code>username</code> when updating</p>
      <p><strong>Optional field names:</strong> all of these are completely optional. If a values is present for the field in the file, then that value is used; else, the default value for that field is used.</p>
      <p><code>institution, department, city, country, lang, auth, ajax, timezone, idnumber, icq, phone1, phone2, address, url, description, mailformat, maildisplay, htmleditor, autosubscribe, emailstop</code></p>
      <p><strong>Custom profile field names:</strong> optional, xxxxx is the real custom user profile field name (i.e. the unique short name)</p>
      <p><code>profile_field_xxxxx</code></p>
      <p><strong>Special field names:</strong> used for changing of usernames and deleting of users, see bellow</p>
      <p><code>deleted, oldusername</code></p>
      <p><strong>Enrolment field names (optional):</strong> The course names are the &quot;shortnames&quot; of the courses - if present
         then the user will be enrolled in those courses. &quot;Type&quot; means type of role to be used for associated course enrolment.
         Value 1 is default course role, 2 is legacy Teacher role and 3 is legacy Non-editing Teacher. You can use role field instead
         to specify roles directly - use either role short name or id (numeric names of roles are not supported). Users may be also
         assigned to groups in course (group1 in course1, group2 in course2, etc.). Groups are again identified by its names or ids
         (numeric names of groups are not supported).</p>
      <p><code>course1, type1, role1, group1, course2, type2, role2, group2, etc.</code></p>
    </blockquote>
    </li>
  <li>Commas within the data should be encoded as &amp;#44 - the script will automatically decode these back to commas.</li>
  <li>For Boolean fields, use 0 for false and 1 for true. </li>
</ul>
<p>Here is an example of a valid import file:</p>
<p><code>username, password, firstname, lastname, email, lang, idnumber, maildisplay, course1, group1, type1<br />
jonest, verysecret, Tom, Jones, jonest@someplace.edu, en, 3663737, 1, Intro101, Section 1, 1<br />
reznort, somesecret, Trent, Reznor, reznort@someplace.edu, en_us, 6736733, 0, Advanced202, Section 3, 3
</code></p>

<h2>Templates</h2>
<p>The default values are processed as templates in which the following codes are allowed:</p>
<ul>
<li><code>%l</code> - will be replaced by the lastname</li>
<li><code>%f</code> - will be replaced by the firstname</li>
<li><code>%u</code> - will be replaced by the username</li>
<li><code>%%</code> - will be replaced by the %</li>
</ul>
<p>Between the percent sign (%) and any code letter (l, f or u) the following modifiers are allowed:</p>
<ul>
<li>(-) minus sign - the information specified by the code letter will be converted to lowercase</li>
<li>(+) plus sign - the information specified by the code letter will be converted to UPPERCASE</li>
<li>(~) tilde sign - the information specified by the code letter will be converted to Title Case</li>
<li>a decimal number - the information specified by the code letter will be truncated to that many characters</li>
</ul>

<p>For example, if the firstname is John and the lastname is Doe, the following values will be obtained with the specified templates:</p>
<ul>
<li>%l%f = DoeJohn</li>
<li>%l%1f = DoeJ</li>
<li>%-l%+f = doeJOHN</li>
<li>%-f_%-l = john_doe</li>
<li>http://www.example.com/~%u/ = http://www.example.com/~jdoe/ (if the username is jdoe or %-1f%-l)</li>
</ul>
<p>Template processing is done only on default values, and not on the values retrieved from the CSV file.</p>
<p>In order to create correct Moodle usernames, the username is always converted to lowercase. Moreover, if the &quot;Allow extended characters in usernames&quot; option in the Site policies page is off, characters different to letters, digits, dash (-) and dot (.) are removed. 
For example if the firstname is John Jr. and the lastname is Doe, the username %-f_%-l will produce john jr._doe when Allow extended characters in usernames is on, and johnjr.doe when off.</p>
<p>When the &quot;New username duplicate handling&quot; setting is set to Append counter, an auto-increment counter will be append to duplicate usernames produced by the template.
For example, if the CSV file contains the users named John Doe, Jane Doe and Jenny Doe without explicit usernames, the default username is %-1f%-l and New username duplicate handling is set to Append counter, then the usernames produced will be jdoe, jdoe2 and jdoe3.
</p>

<h2>Updating existing accounts</h2>

<p>By default Moodle assumes that you will be creating new user accounts, and skips records where the username matches an existing account. However, if you allow updating, the existing user account will be updated. </p>

<p>When updating existing accounts you can change usernames as well. Set "Allow renames" to <b>Yes</b> and include in your file a field called <code>oldusername</code>.</p>

<p><b>Warning:</b> any errors updating existing accounts can affect your users badly. Be careful when using the options to update.</p>

<h2>Deleting accounts</h2>
<p>If the <code>deleted</code> field is present, users with value 1 for it will be deleted. In this case, all the fields may be omitted, except for <code>username</code>.</p>
<p>Deleting and uploading accounts could be done with a single CSV file. For example, the following file will add the user Tom Jones and delete the user reznort:</p>
<p><code>username, firstname, lastname, deleted<br />
jonest, Tom, Jones, 0<br />
reznort, , , 1
</code></p>
